#include "stdafx.h"
#include "DuiDemo.h"
#include "CPageContainer.h"
#include "CViewContainer.h"

const TCHAR* const kClassName = _T("PageContainer");

CPageContainer::CPageContainer()
	: m_pageH(0)
{

}

CPageContainer::~CPageContainer()
{

}

LPCTSTR CPageContainer::GetClass() const
{
	return kClassName;
}

LPVOID CPageContainer::GetInterface(LPCTSTR pstrName)
{
	if (_tcscmp(pstrName, kClassName) == 0) return static_cast<CPageContainer*>(this);
	return CContainerUI::GetInterface(pstrName);
}

void CPageContainer::SetPos(RECT rc, bool bNeedInvalidate/* = true*/)
{
	if (m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible())
	{
		RECT rcBar = rc;
		RECT rcBorder = GetBorderSize();
		int barW = m_pVerticalScrollBar->GetFixedWidth();
		rcBar.left = rcBar.right - barW - rcBorder.right;
		rcBar.right = rcBar.left + barW;
		rcBar.top += rcBorder.top;
		rcBar.bottom -= rcBorder.bottom;
		m_pVerticalScrollBar->SetPos(rcBar, false);
	}

	CContainerUI::SetPos(rc, bNeedInvalidate);
}

void CPageContainer::SetScrollPos(SIZE szPos)
{
	if (m_pageH == 0)
		return;

	CContainerUI::SetScrollPos(szPos);

	int pos = m_pVerticalScrollBar->GetScrollPos();
	int unit = m_pageH;

	int idx = pos / unit;
	g_pMainFrame->m_pContainer->DisplayPage(idx);
}

void CPageContainer::ShowScrollBar()
{
	if (m_pVerticalScrollBar == NULL)
	{
		EnableScrollBar();
		LPCTSTR pDefaultAttributes = g_pMainFrame->GetManager()->GetDefaultAttributeList(_T("VScrollBar"));
		if (pDefaultAttributes) {
			m_pVerticalScrollBar->SetAttributeList(pDefaultAttributes);
		}
	}

	int nPageNum = g_pMainFrame->m_pContainer->GetPageCount();
	int nPageIdx = g_pMainFrame->m_pContainer->GetPageIndex();
	if (nPageIdx == -1 || nPageNum < 2)
	{
		m_pVerticalScrollBar->SetVisible(false);
		m_pVerticalScrollBar->NeedParentUpdate();
		return;
	}
		
	m_pVerticalScrollBar->SetVisible(true);

	RECT rc = GetPos();
	m_pageH = rc.bottom - rc.top;
	m_pVerticalScrollBar->SetScrollRange((nPageNum-1)*m_pageH);
	m_pVerticalScrollBar->SetScrollUnit(m_pageH);
	m_pVerticalScrollBar->SetScrollPos(nPageIdx*m_pageH);
	m_pVerticalScrollBar->NeedParentUpdate();

}
